#include "option.h"
#include <cstring>
#include <iostream>

using namespace std;

void Options::Parse(
    /* [in] */ int argc,
    /* [in] */ char **argv) {

    if (argc <= 1) {
        mIllegalOptions = "\1";
        return;
    }

    mProgram = argv[0];

    int i = 1;
    while (i < argc) {
        string option(argv[i++]);
        if (option == "-genuuid") {
            mShowUriString = true;
        }
        else if (option == "--help") {
            mShowUsage = true;
        }
        else if (option == "-cityhash") {
            mShowHashString = true;

            if (i < argc) {
                mHashString = string(argv[i++]);
            }
            else {
                mIllegalOptions += string("You lost URI! comouuid -cityhash URI");
            }
        }
        else if (option == "--cpp") {
            mCppFormat = true;
        }
        else {
            mIllegalOptions += option;
            mIllegalOptions += ":";
        }
    }
}

bool Options::HasErrors() const
{
    if (mIllegalOptions.length() > 0) {
        if (mIllegalOptions.c_str()[0] == '\1')
            return true;

        printf("%s\n", mIllegalOptions.c_str());
        return true;
    }

    return false;
}

void Options::ShowErrors() const
{
    printf("Use \"--help\" to show usage.\n");
}

void Options::ShowUsage() const {
    printf("Ggenerate a uuid or a hash uuid-style string of a URI (example: "
           "a::b::c).\n"
           "Usage: comouuid [options]\n"
           "Options:\n"
           "--help                  Display command line options\n"
           "--cpp                   Display UUID for C++ program\n"
           "-genuuid                Generate a random hash uuid-style string\n"
           "-cityhash URL           Generate a hash uuid-style string of a URI "
           "(example: a::b::c)\n");
}
